<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_group extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('m_group', '', TRUE);
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index(){
		if($this->auth->is_logged_in() == false){
			redirect('c=c_login','refresh');
		}
		$this->load->view('main/v_group');
	}
	
	function group_list(){
		$result=$this->m_group->group_list();
		echo $result;
	}
	
	function get_permission(){
		$result=$this->m_group->get_permission();
		echo $result;
	}
	
	function get_permission_bygroup(){
		$group_id = isset($_POST['group_id']) ? @$_POST['group_id'] : @$_GET['group_id'];
		$result=$this->m_group->get_permission_bygroup($group_id);
		echo $result;
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "CREATE":
				$this->create();
				break;
			case "UPDATE":
				$this->update();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	function create(){
		$group_name=trim(@$_POST["group_name"]);
		$group_name=htmlentities($group_name,ENT_QUOTES);
		$group_desc=trim(@$_POST["group_desc"]);
		$group_desc=htmlentities($group_desc,ENT_QUOTES);
		$group_active=trim(@$_POST["group_active"]);
		$group_active=htmlentities($group_active,ENT_QUOTES);
		$array_perm_menu=@$_POST["perm_menu"];
		$array_menu_parent=@$_POST["menu_parent"];
		
		$result=$this->m_group->create($group_name, $group_desc, $group_active, $array_perm_menu, $array_menu_parent);
		echo $result;
	}
	
	function update(){
		$group_id=@$_POST["group_id"];
		$group_id=is_numeric($group_id)?$group_id:0;
		$group_name=trim(@$_POST["group_name"]);
		$group_name=htmlentities($group_name,ENT_QUOTES);
		$group_desc=trim(@$_POST["group_desc"]);
		$group_desc=htmlentities($group_desc,ENT_QUOTES);
		$group_active=trim(@$_POST["group_active"]);
		$group_active=htmlentities($group_active,ENT_QUOTES);
		$array_perm_menu=@$_POST["perm_menu"];
		$array_menu_parent=@$_POST["menu_parent"];
		
		$result=$this->m_group->update($group_id, $group_name, $group_desc, $group_active, $array_perm_menu, $array_menu_parent);
		echo $result;
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */